//Copyright (C) 2013 Julio Espindola
//
//This file is part of VSize Checkers.
//
//VSize Checkers is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//VSize Checkers is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with VSize Checkers.  If not, see <http://www.gnu.org/licenses/>.

package checkers;

import java.util.ArrayList;

class Pawn extends Piece
{  
    Pawn(byte color)
    {
        super(color);
    }
    
    @Override
    protected ArrayList<Move> getMoves(Board b, int square)
    {
        int lim = b.isBlack(square)?Board.NUM_DIR:1;
        byte edgeTest = (color&BLACK)!=0?Board.LOWER_EDGE:Board.UPPER_EDGE;
        ArrayList<Move> moves = new ArrayList<Move>(2);
        
        for(int i=b.isBlack(square)?2:0; i<=lim; ++i)
        {
            int dest = b.getIndex(square, i);
            if(dest != -1 && b.isFree(dest))
            {
                boolean crowning = (b.getSquare(dest)&edgeTest)!=0?true:false;
                moves.add(new Move(square, dest, crowning));
            }
        }
        
        return moves;
    }

    @Override
    protected void getJumps(Board b, int square, ArrayList<Move> arr, MJumps jumps)
    {
        ArrayList<Jump> moves = getJumps(b, jumps, square);
        
        if(moves.isEmpty())
        {
            if(!jumps.jumps.isEmpty())
            {
                byte edgeTest = (color&BLACK)!=0?Board.LOWER_EDGE:Board.UPPER_EDGE;
                boolean crowning = (b.getSquare(square)&edgeTest)!=0?true:false;
                if(jumps.jumps.size() > maxSize) maxSize = jumps.jumps.size();
                arr.add(new MJumps(square, jumps, crowning));
            }
            return;
        }
        
        for(int i=0; i<moves.size(); ++i)
        {
            Jump move = moves.get(i);
            
            move.make(b);
            jumps.addJump(move);
            getJumps(b, move.dest, arr, jumps);
            jumps.removeJump();
            move.undo(b);
        }
    }
    
    private ArrayList<Jump> getJumps(Board b, MJumps mjumps, int square)
    {
        ArrayList <Jump> jumps = new ArrayList<Jump>(4);
        
        for(int i=0; i<=Board.NUM_DIR; ++i)
        {
            int capSquare = b.getIndex(square, i);
            if(capSquare!=-1 && !b.isFree(capSquare) && b.jumpable(capSquare, i, color) && !mjumps.alreadyJumped(capSquare))
                jumps.add(new Jump(square, b.getIndex(capSquare, i), capSquare, b.getSquare(capSquare)));
        }
        
        return jumps;
    }
}
